"use strict";
const BaseService = require("./base");
const dayjs = require("dayjs");

const mkdirp = require("mkdirp");
const path = require("path");

class CommonService extends BaseService {
  constructor(...args) {
    super(...args);
    this.model = "tag";
  }

  async getUploadFile(filename, fileDir = "") {
    // 1、获取当前日期     20180920
    let day = dayjs(new Date()).format("YYYYMMDD");
    // 2、创建图片保存的路径
    let dir = path.join(this.config.static.uploadDir + fileDir, day);
    await mkdirp(dir); // 不存在就创建目录
    let date = Date.now(); /* 毫秒数*/
    // 返回图片保存的路径
    let uploadDir = path.join(dir, date + path.extname(filename));
    // app\public\admin\upload\20190914\1536895331444.png
    console.log("filename", filename);
    console.log(dir, date + path.extname(filename));
    console.log(uploadDir);
    return {
      uploadDir,
      saveDir: this.ctx.origin + uploadDir.slice(3).replace(/\\/g, "/"),
    };
  }
}

module.exports = CommonService;
